<?php

/**
 * Created by PhpStorm
 * User Niu Yong Qiang
 * Date 2021/9/18 15:05
 */

namespace App\Models;



use Illuminate\Database\Eloquent\Relations\HasMany;

class SysRoleModel extends BaseModel
{
    protected $table = 'info_sys_roles';
    public $timestamps = false;

    /**
     * 获取角色列表
     * @param $where
     * @param $columns
     * @return array
     */
    public static function getRoleAll($where, $columns = ['*']): array
    {
        $data = [];
        if(!empty($where)) {
            $obj = SysRoleModel::query()
                ->where($where)
                ->select($columns)
                ->orderBy('id', 'desc')
                ->get();
            $data = $obj ? $obj->toArray() : [];
        }
        return $data;
    }

    /**
     * 获取角色
     * @param $where
     * @param $columns
     * @return array
     */
    public static function getRoleDetail($where, $columns = ['*']): array
    {
        $data = [];
        if(!empty($where)) {
            $obj = SysRoleModel::query()
                ->where($where)
                ->select($columns)
                ->first();
            $data = $obj ? $obj->toArray() : [];
        }
        return $data;
    }

    public function permissionId():? HasMany
    {
        return $this->hasMany(SysRolePermissionsModel::class,'role_id','id');
    }

    public function user():? HasMany
    {
        return $this->hasMany(SysUserRoleModel::class,'role_id','id');
    }

    /**
     * 获取用户
     */
    public static function getOneByDeWeight($whereNotId, $whereOrOne): array
    {
        $query = self::query();

        if (!empty($whereNotId)){
            $query->whereNotIn('id', $whereNotId);
        }

        $query->where('deleted_at', '=', 0);

        $query->where(function ($queryTran) use ($whereOrOne) {
            if (!empty($whereOrOne)){
                $queryTran->orWhere('role_name', '=', $whereOrOne);
            }

        });

        $user =  $query->get();
        return $user ? $user->toArray() : [];
    }
}
